<?php

/**
 * This is the model class for table "post".
 *
 * The followings are the available columns in table 'post':
 * @property string $id
 * @property string $post_author
 * @property string $post_title
 * @property string $post_content
 * @property integer $group_id
 * @property integer $featured
 * @property integer $image_link
 * @property string $created
 * @property string $modified
 */
class Post extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'post';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('post_content, group_id', 'required'),
			array('group_id, featured', 'numerical', 'integerOnly'=>true),
			array('post_author', 'length', 'max'=>11),
			array('post_title', 'length', 'max'=>200),
			array('created, modified', 'safe'),
			array('image_link','file',
				'types'=>'jpg,gif,png',
				'maxSize'=> 1024 * 1024 * 10,
				'tooLarge'=>'Anh tai len lon hon 10M, tai lai anh khac nhanh khong thi bao.',
				'allowEmpty'=>true,
				'on'=>'insert,update',
			),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, post_author, post_title, post_content, group_id, featured, image_link, created, modified', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'author'=>array(self::BELONGS_TO, 'User', 'post_author'),
			'group_post'=>array(self::BELONGS_TO, 'GroupPost', 'group_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'post_author' => 'Post Author',
			'post_title' => 'Post Title',
			'post_content' => 'Post Content',
			'group_id' => 'Group',
			'featured' => 'Featured',
			'image_link' => 'Image Link',
			'created' => 'Created',
			'modified' => 'Modified',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('post_author',$this->post_author,true);
		$criteria->compare('post_title',$this->post_title,true);
		$criteria->compare('post_content',$this->post_content,true);
		$criteria->compare('group_id',$this->group_id);
		$criteria->compare('featured',$this->featured);
		$criteria->compare('image_link',$this->image_link);
		$criteria->compare('created',$this->created,true);
		$criteria->compare('modified',$this->modified,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'sort'=>array(
				'defaultOrder'=>'id DESC',
			),
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Post the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function behaviors()
	{
		return array('CTimestampBehavior' => array(
				'class' => 'zii.behaviors.CTimestampBehavior',
				'createAttribute' => 'created',
				'updateAttribute' => 'modified',
				'setUpdateOnCreate' => 'true',
			),
		);
	}

	public function getFeaturedPost(){
		$options = "select * from post where featured is not null";
		return $this->query($options);
	}

	/**
	* @return 5 bài posts để hiển thị trong slide-show
	*/
	function getPostSlideShow()
	{
		$options['select'] = 'id, post_title, post_content, image_link';
		$options['order'] = 'rand()';
		$options['limit'] = '5';

		$data = Post::model()->findAll($options);
		return $data;
	}

	/**
	* @param limit
	* @return trả về số bài posts mới nhất dùng trong phần view bài viết mới trong trang home.php.
	*/
	function gitPostNew($limit = null)
	{
		$options['select'] = 'id, post_title, image_link';
		$options['order'] = 'created DESC';
		$options['limit'] = $limit;

		$data = Post::model()->findAll($options);
		return $data;
	}

	/**
	* @return posts to show in post/index 
	*/
	function getPostShowPostIndex($limit = null, $offset = null, $group_id = null)
	{
		// die(var_dump($group_id));
		$options = new CDbCriteria;
			$options->select = 't.post_title, t.post_content, t.image_link, t.created';
            $options->limit = $limit;
            $options->offset = $offset;
            $options->order='t.created DESC';

    	if ($group_id != null) {
	        return Post::model()->with(array(
				    'author'=>array('select'=>'author.username'),
				    'group_post'=>array(
				    	'select'=>'group_post.group_name',
					    	'condition'=>'group_post.id = '.$group_id
				    ),
				))->findAll($options);
	    } else {
	    	return Post::model()->with(array(
				    'author'=>array('select'=>'author.username'),
				    'group_post'=>array('select'=>'group_post.group_name'),
				))->findAll($options);
	    }
	}
}
